package com.aat.dizena.base.web.controller;

import java.util.*;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.aat.dizena.base.bean.User;
import com.aat.dizena.base.bean.UserRess;
import com.aat.dizena.base.service.UserRessService;
import com.aat.dizena.base.service.UserService;

/**
 * @author wuxin
 * @email 1066031245@qq.com
 * @version v1.0
 * @date 2016年8月30日 上午9:14:56
 * @update no
 * @info
 */
@Controller
@Scope("prototype")
@RequestMapping("test")
public class TestJQgridController {
	@Autowired
	private UserService userServiceImpl;

	@RequestMapping(value = "list.do", method = RequestMethod.GET)
	public String list() {

		return "/user/test";
	}

	// 用户列表查询,返回JSON数据
	@RequestMapping(value = "list.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> list(@RequestParam(value = "page", required = false, defaultValue = "1") int start,
			@RequestParam(value = "rows", required = false, defaultValue = "10") int perpage,
			@RequestParam(value = "sidx", required = false) String sortFiled,
			@RequestParam(value = "sord", required = false) String direction) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", (start - 1) * perpage);
		map.put("perpage", perpage);
		if (StringUtils.isNotEmpty(sortFiled)) {
			map.put("sortFiled", sortFiled);
		} else {
			map.put("sortFiled", "ts");
		}
		if (StringUtils.isNotEmpty(direction)) {
			map.put("direction", direction);
		} else {
			map.put("direction", "DESC");
		}

		List<User> lists = userServiceImpl.queryAll(map);
		Long count = userServiceImpl.queryCount(map);
		Long pages = count % perpage == 0 ? count / perpage : count / perpage + 1;
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("rows", lists);// 数据
		json.put("total", pages);// 总页数
		json.put("records", count);// 总数
		json.put("page", start);// 第几页
		return json;
	}

	@Autowired
	private UserRessService userressService;

	// 用户列表查询,返回JSON数据
	@RequestMapping(value = "test")
	@ResponseBody
	public Map<String, Object> aall(@RequestParam(value = "page", required = false, defaultValue = "1") int start,
			@RequestParam(value = "rows", required = false, defaultValue = "10") int perpage,
			@RequestParam(value = "sidx", required = false) String sortFiled,
			@RequestParam(value = "sord", required = false) String direction) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", (start - 1) * perpage);
		map.put("perpage", perpage);
		if (StringUtils.isNotEmpty(sortFiled)) {
			map.put("sortFiled", sortFiled);
		} else {
			map.put("sortFiled", "ts");
		}
		if (StringUtils.isNotEmpty(direction)) {
			map.put("direction", direction);
		} else {
			map.put("direction", "DESC");
		}

		List<UserRess> lists = userressService.queryAll(map);
		Long count = userressService.queryCount(map);
		Long pages = count % perpage == 0 ? count / perpage : count / perpage + 1;
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("rows", lists);// 数据
		json.put("total", pages);// 总页数
		json.put("records", count);// 总数
		json.put("page", start);// 第几页
		return json;
	}
}
